package com.gsmr.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.gsmr.entity.PageBean;
import com.gsmr.entity.SDCCH;
import com.gsmr.service.SDCCHService;
import com.gsmr.util.DateUtil;
import com.gsmr.util.ExcelUtil;
import com.gsmr.util.FormatDate;
import com.gsmr.util.PropertityUtil;
import com.gsmr.util.ResponseUtil;
import com.gsmr.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * SDCCH Controller层
 * 
 * @author user
 *
 */
@Controller
@RequestMapping("/sdcch")
public class SDCCHController {

	@Resource
	private SDCCHService sdcchService;
	
	List<SDCCH> sdcchList;
	
	/**
	 * 分页条件查询SDCCH
	 */
	@RequestMapping("/list")
	public void list(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime,
			@RequestParam(value = "IMSI", required = false) String IMSI,
			@RequestParam(value = "MSISDN", required = false) String MSISDN,
			@RequestParam(value = "CTCS_ID", required = false) String CTCS_ID,
			@RequestParam(value = "UmType", required = false) String UmType,
			@RequestParam(value = "district", required = false) String district,
			@RequestParam(value = "kilometerStart", required = false) String kilometerStart,
			@RequestParam(value = "kilometerEnd", required = false) String kilometerEnd, SDCCH sdcch,
			@RequestParam(value = "search", required = false) String search,
			HttpServletResponse response) throws Exception {
		if (search == null) return;

		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("FrontProcessorId", StringUtil.formatLike(sdcch.getFrontProcessorId())); // 查询SDCCH名获取
		map.put("IMSI", IMSI);
		map.put("MSISDN", MSISDN);
		map.put("CTCS_ID", CTCS_ID);
		map.put("UmType", UmType);
		map.put("district", district);
		map.put("kilometerStart", kilometerStart);
		map.put("kilometerEnd", kilometerEnd);
		//map.put("start", pageBean.getStart());
		//当天00:00的时间戳
		Long earlyMorningTime = System.currentTimeMillis()/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();
		//当前时间戳
		Long nowTime = System.currentTimeMillis();
		if(startTime!=null&&!startTime.equals("NaN")){
			map.put("startTime", DateUtil.longToString(Long.parseLong(startTime),"yyyyMMdd"));
		}
		else {
			 map.put("startTime", DateUtil.longToString(earlyMorningTime,"yyyyMMdd"));
			 System.out.println("sdcch hahah:" +  DateUtil.longToString(earlyMorningTime,"yyyyMMdd"));
		}
		if(endTime!=null&&!endTime.equals("NaN")){
			map.put("endTime", DateUtil.longToString(Long.parseLong(endTime),"yyyyMMdd"));
		}
		else {
			map.put("endTime", DateUtil.longToString(nowTime,"yyyyMMdd"));
		}
		
		List<String> table_names = sdcchService.findTableNames(map);
		if(startTime!=null&&!startTime.equals("NaN")){
			map.put("startTime", startTime);
		}
		else {
			 map.put("startTime", null);
		}
		if(endTime!=null&&!endTime.equals("NaN")){
			map.put("endTime", endTime);
		}
		else {
			map.put("endTime", null);
		}

		sdcchList = new ArrayList<>();
		
		if(table_names!=null){
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
			int curTableSize;
			for(int i=0;i<table_names.size()&&sdcchList.size() <pageBean.getPageSize();i++){
				String tableName = table_names.get(i);
				map.put("table_name", tableName);
				
				List<SDCCH> sub_sdcchList = sdcchService.find(map);
				curTableSize = sub_sdcchList.size();
				if(curTableSize==(int)map.get("size")){
					sdcchList.addAll(sub_sdcchList);
				}else if(curTableSize<(int)map.get("size")&&curTableSize>0){
					sdcchList.addAll(sub_sdcchList);
					map.put("size", (int)map.get("size")-curTableSize);
					map.put("start", 0);
				}
			}
		}
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(sdcchList);
		System.out.println(" SDCCH jsonArray Size:" + jsonArray.size());
		System.out.println("sdcchList:" + sdcchList);
		result.put("rows", jsonArray);
		result.put("total", Integer.MAX_VALUE);
		ResponseUtil.write(response, result);
		
	}

	/**
	 * 判断是否存在指定PrimaryKey
	 */
	@RequestMapping("/existPrimaryKey")
	public void existUserName(String primaryKey, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		String frontProcessorId = primaryKey.substring(0, primaryKey.indexOf("-"));
		map.put("table_name", "sdcch_" + frontProcessorId);
		map.put("PrimaryKey", primaryKey);
		if (sdcchService.findByPrimaryKey(map) == null) {
			result.put("exist", false);
		} else {
			result.put("exist", true);
		}
		ResponseUtil.write(response, result);
		
	}

	/**
	 * 添加或者修改SDCCH
	 * @param flag 1 添加 2修改
	 */
	@RequestMapping("/save")
	public void save(SDCCH sdcch, HttpServletResponse response, Integer flag) throws Exception {
		int resultTotal = 0; // 操作的记录条数
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("table_name", "sdcch_" + sdcch.getFrontProcessorId());
		map.put("PrimaryKey", sdcch.getPrimaryKey());
		map.put("TimeStamp", sdcch.getTimeStamp());
		map.put("StartTime", sdcch.getStartTime());
		map.put("FrontProcessorId", sdcch.getFrontProcessorId());
		map.put("RecordId", sdcch.getRecordId());
		map.put("IMSI", sdcch.getTMSI());
		map.put("LogicChannel", sdcch.getLogicChannel());
		map.put("MSISDN", sdcch.getMSISDN());
		map.put("Direction", sdcch.getDirection());
		map.put("FrameNumber", sdcch.getFrameNumber());
		map.put("SiteName", sdcch.getSiteName());
		map.put("Decode", sdcch.getDecode());
		map.put("RawMessage", sdcch.getRawMessage());
		map.put("BCCH", sdcch.getBCCH());
		map.put("Layer", sdcch.getLayer());
		map.put("Length", sdcch.getLength());
		map.put("Data", sdcch.getData());
		map.put("Kilometer", sdcch.getKilometer());
		map.put("Speed", sdcch.getSpeed());
		map.put("CI", sdcch.getCI());
		map.put("LAC", sdcch.getLAC());
		map.put("CTCS_ID", sdcch.getCTCS_ID());
		map.put("ENGINE_ID", sdcch.getENGINE_ID());
		map.put("MS_TYPE", sdcch.getMS_TYPE());
		map.put("Message_TYPE", sdcch.getMessage_TYPE());
		map.put("SourceData", sdcch.getSourceData());
		map.put("ARFCN", sdcch.getARFCN());
		map.put("RA", sdcch.getRA());
		map.put("RF_Capability", sdcch.getRF_Capability());
		map.put("TMSI_IMSI", sdcch.getTMSI_IMSI());
		map.put("Call_BCD_Num", sdcch.getCall_BCD_Num());
		map.put("LU_Type", sdcch.getLU_Type());
		map.put("TimeSlot", sdcch.getTimeSlot());
		map.put("TMSI", sdcch.getTMSI());
		map.put("Channel", sdcch.getChannel());
		map.put("TCH", sdcch.getTCH());
		map.put("IMEI", sdcch.getIMEI());
		map.put("L2", sdcch.getL2());
		if (flag == 1) {
			resultTotal = sdcchService.add(map);
		} else {
			resultTotal = sdcchService.update(map);
		}
		JSONObject result = new JSONObject();
		if (resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		
	}

	/**
	 * 删除SDCCH
	 */
	@RequestMapping("/delete")
	public void delete(@RequestParam(value = "primaryKeys") String primaryKeys, HttpServletResponse response)
			throws Exception {
		String[] primaryKeysStr = primaryKeys.split(",");
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject result = new JSONObject();
		try {
			for (int i = 0; i < primaryKeysStr.length; i++) {
				String frontProcessorId = primaryKeysStr[i].substring(0, primaryKeysStr[i].indexOf("-"));
				map.put("table_name", "sdcch_" + frontProcessorId);
				map.put("PrimaryKey", primaryKeysStr[i]);
				sdcchService.delete(map);
			}
			result.put("success", true);
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		ResponseUtil.write(response, result);
	}

	/**
	 * 导出SDCCH
	 */
	@RequestMapping("/export")
	public void export(@RequestParam(value = "ids") String ids, HttpServletResponse response) throws Exception {

		Workbook wb = new HSSFWorkbook();
		String headers[] = { "触发时间", "前置机号", "记录号", "IMSI", "逻辑信道", "MSISDN", "方向", "小区名称", "解码信息", "CI", "LAC", "机车号", "终端类型",
				"Um接口消息类型", "时隙号", "TMSI", "Channel", "TCH", "Layer2类型","数据长度", "数据源码" };

		if (ids != null && !ids.equals("null") && !ids.equals("")) {
			ExcelUtil.fillExcelData_SDCCH(sdcchList, wb, headers);
			ResponseUtil.export(response, wb, "导出excel" + FormatDate.formatDate() + ".xls");
		} else {
			List<SDCCH> sdcchList_all = new ArrayList<SDCCH>();
			List<SDCCH> sub_sdcchList = null;
			Map<String, Object> map = new HashMap<String, Object>();
			List<String> table_names = sdcchService.findTableNames(map);
			if (table_names != null) {
				for (int i = 0; i < table_names.size()&&sdcchList_all.size()<=10000; i++) {
					sub_sdcchList = null;
					map.put("table_name", table_names.get(i));
					sub_sdcchList = sdcchService.find(map);
					if (sub_sdcchList != null) {
						sdcchList_all.addAll(sub_sdcchList);
					}
				}
			}
			ExcelUtil.fillExcelData_SDCCH(sdcchList_all, wb, headers);
			ResponseUtil.export(response, wb, "导出excel" + FormatDate.formatDate() + ".xls");
		}
	}

	/**
	 * 用模板导出用户
	 */
	@RequestMapping("/exportByTemplate")
	public void exportByTemplate(@RequestParam(value = "ids") String ids, HttpServletResponse response)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SDCCH> sdcchList = new ArrayList<SDCCH>();
		if (ids != null && !ids.equals("null") && !ids.equals("")) {
			List<String> idsList = Arrays.asList(ids.split(","));
			map.put("ids", idsList);
			sdcchList.clear();
			List<SDCCH> sub_sdcchList = null;
			List<String> table_names = sdcchService.findTableNames(map);
			if(table_names!=null){
				for (int i = 0; i < table_names.size(); i++) {
					sub_sdcchList = null;
					map.put("table_name", table_names.get(i));
					sub_sdcchList = sdcchService.findByIds(map);
					if (sub_sdcchList != null) {
						sdcchList.addAll(sub_sdcchList);
					}
				}
			}
			Workbook wb=ExcelUtil.fillExcelDataWithTemplate_SDCCH(sdcchList,"sdcchExportTemplate.xls");
			ResponseUtil.export(response, wb, "利用模版导出excel"+FormatDate.formatDate()+".xls");
		} else {
			sdcchList.clear();
			List<SDCCH> sub_sdcchList = null;
			List<String> table_names = sdcchService.findTableNames(map);
			if (table_names != null) {
				for (int i = 0; i < table_names.size(); i++) {
					sub_sdcchList = null;
					map.put("table_name", table_names.get(i));
					sub_sdcchList = sdcchService.find(map);
					if (sub_sdcchList != null) {
						sdcchList.addAll(sub_sdcchList);
					}
				}
			}
			Workbook wb=ExcelUtil.fillExcelDataWithTemplate_SDCCH(sdcchList,"sdcchExportTemplate.xls");
			ResponseUtil.export(response, wb, "利用模版导出excel"+FormatDate.formatDate()+".xls");
		}
	}

	/**
	 * 用户上传
	 */
	@RequestMapping("/upload")
	public void upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 将当前上下文初始化给 CommonsMutipartResolver （多部分解析器）
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
					request.getSession().getServletContext());
			// 检查form中是否有enctype="multipart/form-data"
			if (multipartResolver.isMultipart(request)) {
				// 将request变成多部分request
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				// 获取multiRequest 中所有的文件名
				Iterator iter = multiRequest.getFileNames();

				while (iter.hasNext()) {
					// 一次遍历所有文件
					MultipartFile file = multiRequest.getFile(iter.next().toString());
					if (file != null) {
						String path = PropertityUtil.UPLOAD_PATH + FormatDate.formatDate() + file.getOriginalFilename();
						// 上传
						File newFile = new File(path);
						file.transferTo(newFile);
						// 写数据进数据库
						POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(newFile));
						HSSFWorkbook wb = new HSSFWorkbook(fs);
						HSSFSheet hssfSheet = wb.getSheetAt(0); // 获取第一个Sheet页
						if (hssfSheet != null) {
							for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
								HSSFRow hssfRow = hssfSheet.getRow(rowNum);
								if (hssfRow == null) {
									continue;
								}
								SDCCH sdcch = new SDCCH();
								sdcch.setPrimaryKey(ExcelUtil.formatCell(hssfRow.getCell(0)));
								sdcch.setTimeStamp(ExcelUtil.formatCell(hssfRow.getCell(1)));
								sdcch.setStartTime(ExcelUtil.formatCell(hssfRow.getCell(2)));
								sdcch.setFrontProcessorId(ExcelUtil.formatCell(hssfRow.getCell(3)));
								sdcch.setRecordId(ExcelUtil.formatCell(hssfRow.getCell(4)));
								sdcch.setIMSI(ExcelUtil.formatCell(hssfRow.getCell(5)));
								sdcch.setLogicChannel(ExcelUtil.formatCell(hssfRow.getCell(6)));
								sdcch.setMSISDN(ExcelUtil.formatCell(hssfRow.getCell(7)));
								sdcch.setDirection(ExcelUtil.formatCell(hssfRow.getCell(8)));
								sdcch.setFrameNumber(ExcelUtil.formatCell(hssfRow.getCell(9)));
								sdcch.setSiteName(ExcelUtil.formatCell(hssfRow.getCell(10)));
								sdcch.setDecode(ExcelUtil.formatCell(hssfRow.getCell(11)));
								sdcch.setRawMessage(ExcelUtil.formatCell(hssfRow.getCell(12)));
								sdcch.setBCCH(ExcelUtil.formatCell(hssfRow.getCell(13)));
								sdcch.setLayer(ExcelUtil.formatCell(hssfRow.getCell(14)));
								sdcch.setLength(ExcelUtil.formatCell(hssfRow.getCell(15)));
								sdcch.setData(ExcelUtil.formatCell(hssfRow.getCell(16)));
								sdcch.setKilometer(ExcelUtil.formatCell(hssfRow.getCell(17)));
								sdcch.setSpeed(ExcelUtil.formatCell(hssfRow.getCell(18)));
								sdcch.setCI(ExcelUtil.formatCell(hssfRow.getCell(19)));
								sdcch.setLAC(ExcelUtil.formatCell(hssfRow.getCell(20)));
								sdcch.setCTCS_ID(ExcelUtil.formatCell(hssfRow.getCell(21)));
								sdcch.setENGINE_ID(ExcelUtil.formatCell(hssfRow.getCell(22)));
								sdcch.setMS_TYPE(ExcelUtil.formatCell(hssfRow.getCell(23)));
								sdcch.setMessage_TYPE(ExcelUtil.formatCell(hssfRow.getCell(24)));
								sdcch.setSourceData(ExcelUtil.formatCell(hssfRow.getCell(25)));
								sdcch.setARFCN(ExcelUtil.formatCell(hssfRow.getCell(26)));
								sdcch.setRA(ExcelUtil.formatCell(hssfRow.getCell(27)));
								sdcch.setRF_Capability(ExcelUtil.formatCell(hssfRow.getCell(28)));
								sdcch.setTMSI_IMSI(ExcelUtil.formatCell(hssfRow.getCell(29)));
								sdcch.setCall_BCD_Num(ExcelUtil.formatCell(hssfRow.getCell(30)));
								sdcch.setLU_Type(ExcelUtil.formatCell(hssfRow.getCell(31)));
								sdcch.setTimeSlot(ExcelUtil.formatCell(hssfRow.getCell(32)));
								sdcch.setTMSI(ExcelUtil.formatCell(hssfRow.getCell(33)));
								sdcch.setChannel(ExcelUtil.formatCell(hssfRow.getCell(34)));
								sdcch.setTCH(ExcelUtil.formatCell(hssfRow.getCell(35)));
								sdcch.setIMEI(ExcelUtil.formatCell(hssfRow.getCell(36)));
								sdcch.setL2(ExcelUtil.formatCell(hssfRow.getCell(37)));
								map.put("table_name", "sdcch_" + sdcch.getFrontProcessorId());
								map.put("PrimaryKey", sdcch.getPrimaryKey());
								map.put("TimeStamp", sdcch.getTimeStamp());
								map.put("StartTime", sdcch.getStartTime());
								map.put("FrontProcessorId", sdcch.getFrontProcessorId());
								map.put("RecordId", sdcch.getRecordId());
								map.put("IMSI", sdcch.getTMSI());
								map.put("LogicChannel", sdcch.getLogicChannel());
								map.put("MSISDN", sdcch.getMSISDN());
								map.put("Direction", sdcch.getDirection());
								map.put("FrameNumber", sdcch.getFrameNumber());
								map.put("SiteName", sdcch.getSiteName());
								map.put("Decode", sdcch.getDecode());
								map.put("RawMessage", sdcch.getRawMessage());
								map.put("BCCH", sdcch.getBCCH());
								map.put("Layer", sdcch.getLayer());
								map.put("Length", sdcch.getLength());
								map.put("Data", sdcch.getData());
								map.put("Kilometer", sdcch.getKilometer());
								map.put("Speed", sdcch.getSpeed());
								map.put("CI", sdcch.getCI());
								map.put("LAC", sdcch.getLAC());
								map.put("CTCS_ID", sdcch.getCTCS_ID());
								map.put("ENGINE_ID", sdcch.getENGINE_ID());
								map.put("MS_TYPE", sdcch.getMS_TYPE());
								map.put("Message_TYPE", sdcch.getMessage_TYPE());
								map.put("SourceData", sdcch.getSourceData());
								map.put("ARFCN", sdcch.getARFCN());
								map.put("RA", sdcch.getRA());
								map.put("RF_Capability", sdcch.getRF_Capability());
								map.put("TMSI_IMSI", sdcch.getTMSI_IMSI());
								map.put("Call_BCD_Num", sdcch.getCall_BCD_Num());
								map.put("LU_Type", sdcch.getLU_Type());
								map.put("TimeSlot", sdcch.getTimeSlot());
								map.put("TMSI", sdcch.getTMSI());
								map.put("Channel", sdcch.getChannel());
								map.put("TCH", sdcch.getTCH());
								map.put("IMEI", sdcch.getIMEI());
								map.put("L2", sdcch.getL2());
								sdcchService.add(map);
							}
						}
					}
				}
			}
			result.put("success", "true");
		} catch (Exception e) {
			result.put("success", "false");
			e.printStackTrace();
		}
		ResponseUtil.write(response, result);
		
	}

	/**
	 * 查找District列表
	 */
	@RequestMapping("/sdcchDistrictComboList")
	public void sdcchDistrictComboList(HttpServletResponse response) throws Exception {
		Map<String,Object> map=new HashMap<String,Object>();
		JSONArray jsonArray=new JSONArray();
		JSONObject jsonObject=new JSONObject();
		
		JSONArray jsonArray1=new JSONArray();
		JSONObject jsonObject1=new JSONObject();
		
		//System.out.println("开始找sitename" );
		// jsonObject.put("id", -1);
		// jsonObject.put("name", "");
		// jsonArray.add(jsonObject);
		map.put("table_name","bs_info");
		List<String> bs_names=sdcchService.findDistricts(map);
		for(int i=0;i<bs_names.size();i++)
		{
			jsonObject.put("id", i);
			//String bs_name=alarmInfoService.get_bs_names();
			//System.out.println("bs_names:" + bs_names.get(i));
			jsonObject.put("name",bs_names.get(i));
			jsonObject.put("text", bs_names.get(i));
			System.out.println("小区："+bs_names.get(i));
			jsonArray.add(jsonObject);
			
		}
		System.out.println(jsonArray);
		jsonObject1.put("id", 1000);
		jsonObject1.put("name", "全选");
		jsonObject1.put("text", "全选");
		jsonObject1.put("children", jsonArray);
		jsonArray1.add(jsonObject1);
		ResponseUtil.write(response, jsonArray1);
	}

	/**
	 * 查找Message_TYPE列表
	 */
	@RequestMapping("/sdcchMessage_TYPEComboList")
	public void sdcchMessage_TYPEComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> table_names = sdcchService.findTableNames(map);
		List<SDCCH> sdcchList = new ArrayList<SDCCH>();
		if (table_names != null) {
			for (int i = 0; i < table_names.size(); i++) {
				map.put("table_name", table_names.get(i));
				List<SDCCH> sub_sdcchList = sdcchService.findMessage_TYPEs(map);
				sdcchList.addAll(sub_sdcchList);
			}
		}
		
		// 过滤重复的Message_TYPE
		Map message_TYPE_map = new HashMap<>();
		for (int i = 0; i < sdcchList.size(); i++) {
			message_TYPE_map.put(sdcchList.get(i).getMessage_TYPE(), 1);
		}
		Iterator<Map.Entry<String, String>> iterator = message_TYPE_map.entrySet().iterator();
		sdcchList.clear();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			SDCCH sdcch = new SDCCH();
			sdcch.setMessage_TYPE(entry.getKey());
			sdcchList.add(sdcch);
		}
				
		JSONArray rows = JSONArray.fromObject(sdcchList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
		
	}

	/**
	 * 查找前置机列表
	 */
	@RequestMapping("/sdcchFrontProcessorIdComboList")
	public void sdcchFrontProcessorIdComboList(HttpServletResponse response) throws Exception {
		Map<String,Object> map=new HashMap<String,Object>();
		JSONArray jsonArray=new JSONArray();
		JSONObject jsonObject=new JSONObject();
		//System.out.println("开始找sitename" );
		jsonObject.put("id", -1);
		jsonObject.put("name", "");
		jsonArray.add(jsonObject);
		map.put("table_name","bs_info");
		List<String> FrontProcessorIds=sdcchService.findFrontProcessorIds(map);
		for(int i=0;i<FrontProcessorIds.size();i++)
		{
			jsonObject.put("id", i);
			//String bs_name=alarmInfoService.get_bs_names();
			//System.out.println("FrontProcessorIds:" + FrontProcessorIds.get(i));
			jsonObject.put("name",FrontProcessorIds.get(i));
			jsonArray.add(jsonObject);
		}
		ResponseUtil.write(response, jsonArray);
	}

	/**
	 * 查找IMSI列表
	 */
	@RequestMapping("/sdcchIMSIComboList")
	public void sdcchIMSIComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> table_names = sdcchService.findTableNames(map);
		List<SDCCH> sdcchList = new ArrayList<SDCCH>();
		if (table_names != null) {
			for (int i = 0; i < table_names.size(); i++) {
				map.put("table_name", table_names.get(i));
				List<SDCCH> sub_sdcchList = sdcchService.findIMSIs(map);
				sdcchList.addAll(sub_sdcchList);
			}
		}
		
		// 过滤重复的IMSI
		Map iMSI_map = new HashMap<>();
		for (int i = 0; i < sdcchList.size(); i++) {
			iMSI_map.put(sdcchList.get(i).getIMSI(), 1);
		}
		Iterator<Map.Entry<String, String>> iterator = iMSI_map.entrySet().iterator();
		sdcchList.clear();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			SDCCH sdcch = new SDCCH();
			sdcch.setIMSI(entry.getKey());
			sdcchList.add(sdcch);
		}
				
		JSONArray rows = JSONArray.fromObject(sdcchList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
		
	}

	/**
	 * 查找MSISDN列表
	 */
	@RequestMapping("/sdcchMSISDNComboList")
	public void sdcchMSISDNComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> table_names = sdcchService.findTableNames(map);
		List<SDCCH> sdcchList = new ArrayList<SDCCH>();
		if (table_names != null) {
			for (int i = 0; i < table_names.size(); i++) {
				map.put("table_name", table_names.get(i));
				List<SDCCH> sub_sdcchList = sdcchService.findMSISDNs(map);
				sdcchList.addAll(sub_sdcchList);
			}
		}
		
		// 过滤重复的MSISDN
		Map mSISDN_map = new HashMap<>();
		for (int i = 0; i < sdcchList.size(); i++) {
			mSISDN_map.put(sdcchList.get(i).getMSISDN(), 1);
		}
		Iterator<Map.Entry<String, String>> iterator = mSISDN_map.entrySet().iterator();
		sdcchList.clear();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			SDCCH sdcch = new SDCCH();
			sdcch.setMSISDN(entry.getKey());
			sdcchList.add(sdcch);
		}
				
		JSONArray rows = JSONArray.fromObject(sdcchList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
		
	}

	/**
	 * 查找CTCS_ID列表
	 */
	@RequestMapping("/sdcchCTCS_IDComboList")
	public void sdcchCTCS_IDComboList(HttpServletResponse response) throws Exception {
		JSONArray jsonArray = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> table_names = sdcchService.findTableNames(map);
		List<SDCCH> sdcchList = new ArrayList<SDCCH>();
		if (table_names != null) {
			for (int i = 0; i < table_names.size(); i++) {
				map.put("table_name", table_names.get(i));
				List<SDCCH> sub_sdcchList = sdcchService.findCTCS_IDs(map);
				sdcchList.addAll(sub_sdcchList);
			}
		}
		
		// 过滤重复的CTCS_ID
		Map cTCS_ID_map = new HashMap<>();
		for (int i = 0; i < sdcchList.size(); i++) {
			cTCS_ID_map.put(sdcchList.get(i).getCTCS_ID(), 1);
		}
		Iterator<Map.Entry<String, String>> iterator = cTCS_ID_map.entrySet().iterator();
		sdcchList.clear();
		while (iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			SDCCH sdcch = new SDCCH();
			sdcch.setCTCS_ID(entry.getKey());
			sdcchList.add(sdcch);
		}
		JSONArray rows = JSONArray.fromObject(sdcchList);
		jsonArray.addAll(rows);
		ResponseUtil.write(response, jsonArray);
	}
}
